package com.example.springboot_myproject.Service;

import com.example.springboot_myproject.Mapper.StudentMapper;
import com.example.springboot_myproject.Model.Student;
import com.example.springboot_myproject.Model.StudentFuzzyQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServie {
    @Resource
    private StudentMapper studentMapper;

//    查询并分页显示首页信息
    public Map<String,Object> querystudentbyParams(StudentFuzzyQuery studentFuzzyQuery,
                                                   String sname,String sno,String sjob){
        Map<String,Object> map=new HashMap<>();
        PageHelper.startPage(studentFuzzyQuery.getPage(),studentFuzzyQuery.getLimit());
        PageInfo<Student> pageInfo=new PageInfo<>(studentMapper.selectFuzzytianming(studentFuzzyQuery,sname,sno,sjob));
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }

//    添加学生信息,显示添加信息
public List<Map<String, Object>> findSno(Student student) {
    return this.studentMapper.selectStudentBySno(student);
}
    public Map<String,Object> addStudentInfo(Student student){
        student.setSno(student.getSno());
        student.setSname(student.getSname());
        student.setSjob(student.getSjob());
        student.setStel(student.getStel());
        student.setSdid(student.getSdid());
//        判断学号是否存在
        int result = studentMapper.insertStudent(student);
        Map<String,Object> resultmap=new HashMap<>();
        if(result>0){
            resultmap.put("code",200);
            resultmap.put("message","添加成功!");
            return resultmap;
        }else {
            resultmap.put("code", 400);
            resultmap.put("message", "添加失败!");
            return resultmap;
        }
    }
    public Map<String,Object> updateStudent(Student student){
        Map<String,Object> resultmap=new HashMap<>();

        int resultUpdate = studentMapper.UpdateStudent(student);
        if(resultUpdate>0) {
            resultmap.put("code", 200);
            resultmap.put("message", "修改成功啦！！！");
            return resultmap;
        }else {
            resultmap.put("code", 400);
            resultmap.put("message", "修改失败惹！！！");
            return resultmap;
        }
    }

//    删除功能
    public boolean deletebyids(String ids){
        try {
            studentMapper.DeleteById(ids);
            return true;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }
}
